/**
 * 组件加载器
 * 由于 Vite 的动态导入限制，需要预先定义所有可能的组件路径
 */

// 组件路径映射表
const componentMap = {
  // Dashboard
  '/dashboard/index': () => import('@/views/dashboard/index.vue'),
  '/dashboard': () => import('@/views/dashboard/index.vue'),
  
  // System
  '/system/user/index': () => import('@/views/system/user/index.vue'),
  '/system/user': () => import('@/views/system/user/index.vue'),
  '/system/role/index': () => import('@/views/system/role/index.vue'),
  '/system/role': () => import('@/views/system/role/index.vue'),
  '/system/menu/index': () => import('@/views/system/menu/index.vue'),
  '/system/menu': () => import('@/views/system/menu/index.vue'),
  '/system/department/index': () => import('@/views/system/department/index.vue'),
  '/system/department': () => import('@/views/system/department/index.vue'),
  '/system/permission/index': () => import('@/views/system/permission/index.vue'),
  '/system/permission': () => import('@/views/system/permission/index.vue'),
  '/system/operation-log/index': () => import('@/views/system/operation-log/index.vue'),
  '/system/operation-log': () => import('@/views/system/operation-log/index.vue'),
  '/system/storage/index': () => import('@/views/system/storage/index.vue'),
  '/system/storage': () => import('@/views/system/storage/index.vue'),
  '/system/websocket/index': () => import('@/views/system/websocket/index.vue'),
  '/system/websocket': () => import('@/views/system/websocket/index.vue'),
  '/system/env-config/index': () => import('@/views/system/env-config/index.vue'),
  '/system/env-config': () => import('@/views/system/env-config/index.vue'),
  
  // Monitor
  '/monitor/online-user/index': () => import('@/views/monitor/online-user/index.vue'),
  '/monitor/online-user': () => import('@/views/monitor/online-user/index.vue'),
  '/monitor/login-log/index': () => import('@/views/monitor/login-log/index.vue'),
  '/monitor/login-log': () => import('@/views/monitor/login-log/index.vue'),
  '/monitor/system/index': () => import('@/views/monitor/system/index.vue'),
  '/monitor/system': () => import('@/views/monitor/system/index.vue'),
  
  // Profile
  '/profile/index': () => import('@/views/profile/index.vue'),
  '/profile': () => import('@/views/profile/index.vue'),
}

/**
 * 加载组件
 * @param {string} componentPath - 组件路径（如：system/user/index 或 /system/user/index）
 * @returns {Promise} 组件模块
 */
export function loadComponent(componentPath) {
  console.log(`[ComponentLoader] 加载组件: ${componentPath}`)
  
  // 规范化路径
  let normalizedPath = componentPath.trim()
  
  // 如果路径不是以 / 开头，添加 /
  if (!normalizedPath.startsWith('/')) {
    normalizedPath = `/${normalizedPath}`
  }
  
  // 移除末尾的 .vue（如果有）
  normalizedPath = normalizedPath.replace(/\.vue$/, '')
  
  console.log(`[ComponentLoader] 规范化后的路径: ${normalizedPath}`)
  
  // 尝试直接匹配
  if (componentMap[normalizedPath]) {
    console.log(`[ComponentLoader] 找到直接匹配: ${normalizedPath}`)
    return componentMap[normalizedPath]()
      .then(module => {
        console.log(`[ComponentLoader] 成功加载组件: ${normalizedPath}`)
        return module
      })
      .catch(err => {
        console.error(`[ComponentLoader] 加载组件失败: ${normalizedPath}`, err)
        return import('@/views/404.vue')
      })
  }
  
  // 尝试添加 /index 后缀
  const indexPath = normalizedPath.endsWith('/index') 
    ? normalizedPath 
    : `${normalizedPath}/index`
  
  console.log(`[ComponentLoader] 尝试索引路径: ${indexPath}`)
  
  if (componentMap[indexPath]) {
    console.log(`[ComponentLoader] 找到索引匹配: ${indexPath}`)
    return componentMap[indexPath]()
      .then(module => {
        console.log(`[ComponentLoader] 成功加载组件: ${indexPath}`)
        return module
      })
      .catch(err => {
        console.error(`[ComponentLoader] 加载组件失败: ${indexPath}`, err)
        return import('@/views/404.vue')
      })
  }
  
  // 如果都不匹配，返回 404 页面
  console.warn(`[ComponentLoader] 组件路径未找到: ${componentPath} (规范化: ${normalizedPath}, 索引: ${indexPath})，使用 404 页面`)
  console.log(`[ComponentLoader] 可用的组件路径:`, Object.keys(componentMap))
  return import('@/views/404.vue')
}

/**
 * 获取所有组件路径（用于构建时的静态分析）
 */
export function getAllComponentPaths() {
  return Object.keys(componentMap)
}

